import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/page/login'
import index from '@/components/page/index'
import banner from '@/components/page/banner'
import dolls from '@/components/page/dolls'
import dollsBox from '@/components/page/dollsBox'
import user from '@/components/page/user'
import rechargeBox from '@/components/page/rechargeBox'
import recharge from '@/components/page/recharge'
import setRecharge from '@/components/page/setRecharge'
import editRecharge from '@/components/page/editRecharge'
// import addDolls from '@/components/page/addDolls'
import editDolls from '@/components/page/editDolls'
import gameRecord from '@/components/page/gameRecord'
import order from '@/components/page/order'
import countBox from '@/components/page/countBox'
import count from '@/components/page/count'
import countCoin from '@/components/page/countCoin'
import setting from '@/components/page/setting'
import account from '@/components/page/account'

Vue.use(Router)
const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login //登录
    },
    {
      path: '/',
      redirect:'/banner',
      name: 'index',
      component: index,
      meta: {requiresAuth: true},
      children: [
        {
          path: '/banner',
          name: 'banner',
          component: banner, //轮播图
          meta: {requiresAuth: true}
        },
        {
          path: '/dollsBox',
          name: 'dollsBox',
          component: dollsBox, //娃娃机管理
          meta: {requiresAuth: true},
          children: [
            {
              path: '/',
              redirect:'dollsBox/dolls',
            },
            {
              path: 'dollsBox/dolls',
              name: 'dolls',
              component: dolls, //娃娃机管理
              meta: {requiresAuth: true}
            },
            // {
            //   path: '/dollsBox/addDolls',
            //   name: 'addDolls',
            //   component: addDolls, //添加娃娃机
            //   meta: {requiresAuth: true}
            // },
            {
              path: '/dollsBox/editDolls',
              name: 'editDolls',
              component: editDolls, //编辑娃娃机
              meta: {requiresAuth: true}
            }]
        },
        {
          path: '/user',
          name: 'user',
          component: user, //用户管理
          meta: {requiresAuth: true}
        },
        {
          path: '/rechargeBox',
          name: 'rechargeBox',
          component: rechargeBox, //充值管理
          meta: {requiresAuth: true},
          children: [
            {
              path: '/',
              name: 'recharge',
              component: recharge, //充值管理
              meta: {requiresAuth: true}
            },
            {
              path: '/rechargeBox/setRecharge',
              name: 'setRecharge',
              component: setRecharge, //充值设置
              meta: {requiresAuth: true}
            },
            {
              path: '/rechargeBox/editRecharge',
              name: 'editRecharge',
              component: editRecharge, //充值设置
              meta: {requiresAuth: true}
            }]
        },
        {
          path: '/gameRecord',
          name: 'gameRecord',
          component: gameRecord, //游戏记录
          meta: {requiresAuth: true}
        },
        {
          path: '/order',
          name: 'order',
          component: order, //订单管理
          meta: {requiresAuth: true}
        },{
          path: 'countBox',
          redirect:'countBox/count'
        },
        {
          path: 'countBox',
          name: 'countBox',
          component: countBox, //数据统计
          meta: {requiresAuth: true},
          children: [
            {
              path: 'count',
              name: 'count',
              component: count, //数据统计
              meta: {requiresAuth: true}
            },
            {
              path: 'countCoin',
              name: 'countCoin',
              component: countCoin, //金币数据统计
              meta: {requiresAuth: true}
            }]
        },
        {
          path: 'setting',
          name: 'setting',
          component: setting, //后台设置
          meta: {requiresAuth: true}
        },
        {
          path: 'account',
          name: 'account',
          component: account, //账号管理
          meta: {requiresAuth: true}
        },

      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  let token = window.sessionStorage.getItem('token');
  if (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null)) {
    next({
      path: '/login',
      query: {redirect: to.fullPath}
    })
  } else {
    next()
  }

})


export default router;

